/**
 * @file models/featureList.js
 * @author
 */

import { routerRedux } from 'dva/router';
import api from '../api';
import { delay } from '../utils/sagaEffects';

export default {
	namespace: 'featureList',
	state: {
		currentDataDate: new Date(),
		list: [],
		detail: {},
		page: {},
		featuresMapping: [],
		selectedTrader: null,
		isDetailOpen: false,//侧边弹出详情 true/false
		scores: {},
	},
	reducers: {
		getListSuccess(state, action) {
			const { payload: { response } } = action;
			let d = state.currentDataDate;
			if (response.data != null && response.data.length > 0) {
				d = new Date(response.data[0].dataDate);
			}

			return {
				...state,
				currentDataDate: d,
				list: response.data,
				page: response.page,
			};
		},
		getMappingSuccess(state, action) {
			const { payload: { response } } = action;
			return {
				...state,
				featuresMapping: response.data,
			};
		},
		changeTrader(state, action) {
			const { payload: { selectedTrader } } = action;
			return {
				...state,
				isDetailOpen: true,
				selectedTrader,
			};
		},
		changeDetailOpen(state, action) {
			const { payload: { open } } = action;
			return {
				...state,
				selectedTrader: open?state.selectedTrader:undefined,
				isDetailOpen: open,
			};
		},
		getHistoryFeaturesSuccess(state, action) {
			const { payload: { response } } = action;
			return {
				...state,
				historyFeatures: response.data,
			};
		},
		getScoresSuccess(state, action) {
			const { payload: { response } } = action;
			return {
				...state,
				scores: response.data,
			};
		},
	},
	effects: {
		* getFeatureList({ payload: query }, { call, put }) {
			const response = yield call(api.getFeatureList, query);
			yield put({
				type: 'getListSuccess',
				payload: { response },
			});
		},
		* getFeaturesMapping({ payload: query }, { call, put }) {
			const response = yield call(api.getFeaturesMapping, query);
			yield put({
				type: 'getMappingSuccess',
				payload: { response },
			});
		},
		* getHistoryFeatures({ payload: query }, { call, put }) {
			const response = yield call(api.getHistoryFeatures, query);
			yield put({
				type: 'getHistoryFeaturesSuccess',
				payload: { response },
			});
		},
		* getScores({ payload: query }, { call, put }) {
			const response = yield call(api.getScores, query);
			yield put({
				type: 'getScoresSuccess',
				payload: { response },
			});
		},
	},
	subscriptions: {},
};
